/*
 * Project Name: jmax
 * File Name: UserSession.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.shiro.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.xdtech.shiro.session.mgt.SysSession;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-2-6 下午3:53:25
 */
@Entity
@Table(name = "SYS_USER_SESSION")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UserSession
{
	@Id
	@Column(name = "USER_SESSION_ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column(name = "SESSION_ID")
	private String sessionId;
	//当前登录的用户Id
	@Column(name = "USER_ID")
	private Long userId = 0L;

	@Column(name = "USERNAME")
	private String username;

	/**
	 * 用户主机地址
	 */
	@Column(name = "HOST")
	private String host;

	/**
	 * 用户登录时系统IP
	 */
	@Column(name = "SYSTEM_HOST")
	private String systemHost;

	/**
	 * 用户浏览器类型
	 */
	@Column(name = "USER_AGENT")
	private String userAgent;

	/**
	 * 在线状态
	 */
	@Column(name = "STATUS")
	@Enumerated(EnumType.STRING)
	private SysSession.OnlineStatus status = SysSession.OnlineStatus.on_line;

	/**
	 * session创建时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "START_TIMESTSAMP")
	@Temporal(TemporalType.TIMESTAMP)
	private Date startTimestamp;
	/**
	 * session最后访问时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "LAST_ACCESS_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastAccessTime;

	/**
	 * 超时时间
	 */
	@Column(name = "TIMEOUT")
	private Long timeout;

	/**
	 * 备份的当前用户会话
	 */
	@Transient
	private SysSession session;

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public String getSessionId()
	{
		return sessionId;
	}

	public void setSessionId(String sessionId)
	{
		this.sessionId = sessionId;
	}

	public Long getUserId()
	{
		return userId;
	}

	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getHost()
	{
		return host;
	}

	public void setHost(String host)
	{
		this.host = host;
	}

	public String getSystemHost()
	{
		return systemHost;
	}

	public void setSystemHost(String systemHost)
	{
		this.systemHost = systemHost;
	}

	public String getUserAgent()
	{
		return userAgent;
	}

	public void setUserAgent(String userAgent)
	{
		this.userAgent = userAgent;
	}

	public SysSession.OnlineStatus getStatus()
	{
		return status;
	}

	public void setStatus(SysSession.OnlineStatus status)
	{
		this.status = status;
	}

	public Date getStartTimestamp()
	{
		return startTimestamp;
	}

	public void setStartTimestamp(Date startTimestamp)
	{
		this.startTimestamp = startTimestamp;
	}

	public Date getLastAccessTime()
	{
		return lastAccessTime;
	}

	public void setLastAccessTime(Date lastAccessTime)
	{
		this.lastAccessTime = lastAccessTime;
	}

	public Long getTimeout()
	{
		return timeout;
	}

	public void setTimeout(Long timeout)
	{
		this.timeout = timeout;
	}

	public SysSession getSession()
	{
		return session;
	}

	public void setSession(SysSession session)
	{
		this.session = session;
	}

	public static final UserSession fromUserSession(SysSession session)
	{
		UserSession online = new UserSession();
		online.setSessionId(String.valueOf(session.getId()));
		online.setUserId(session.getUserId());
		online.setUsername(session.getUsername());
		online.setStartTimestamp(session.getStartTimestamp());
		online.setLastAccessTime(session.getLastAccessTime());
		online.setTimeout(session.getTimeout());
		online.setHost(session.getHost());
		online.setUserAgent(session.getUserAgent());
		online.setSystemHost(session.getSystemHost());
		online.setSession(session);

		return online;
	}
}
